package com.sensePlusVR.activity.mine;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.db.ImageCache;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.FileUtils;
import com.sensePlusVR.utils.FileUtils.FileCallback;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.widget.CircleDLProgressView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * http://blog.csdn.net/Beyond0525/article/details/48181345
 * 
 * @author: Loren
 * @create: 2016-7-28下午7:37:04
 * @desc: 清理缓存
 * 
 */
@SuppressWarnings("unused")
@ContentView(R.layout.activity_setting_core_clearcache)
public class ClearCacheActivity extends BaseActivity {
	private static final String TAG = ClearCacheActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	@ViewInject(R.id.title_bar_left_ib)
	private ImageView title_bar_left_ib;
	@ViewInject(R.id.circleProgressbar)
	private CircleDLProgressView mProgressView;
	@ViewInject(R.id.clear_cache_tip)
	private TextView clear_cache_tip;
	
	private static final int PERIOD = 1000;
	private static final int DETAY = 0;
	private int progress;
	private long totoalSize = 0;
	private long delTotalSize = 0;
	
	
	List<String> pathList = new ArrayList<String>();
	private String cachePath;
	private String fileRoot;
	
	private long bytesPerMB = 1024*1024;
	
	private int maxCount = 100;
	
	private boolean canBack = false;
	
	
	

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void beforeView() {
		super.beforeView();
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(getResources().getString(R.string.clear_the_cache));
		
		
		cachePath = FileUtils.getExternalCacheDir(this);
		fileRoot = FileUtils.getExternalFilesDir(this);
		
		MyLog.d(TAG, "cahePath="+cachePath);
		MyLog.d(TAG, "fileRoot="+fileRoot);
		
		pathList.add(cachePath + Constant.PATH_CACHE_IMAGES);
		pathList.add(cachePath + Constant.PATH_CACHE_VIDEOS);
		pathList.add(cachePath + Constant.PATH_CACHE_UNITYSHADER);
		pathList.add(fileRoot + Constant.PATH_CACHE_MOVIES);
	
	
		
		
		mProgressView.setCenterTextVisible(true);
		mProgressView.setScoreVisible(false);
		mProgressView.setMaxCount(maxCount);
		mProgressView.setCenterText(getResources().getString(R.string.clean_cache_calculate), "0M");
		clear_cache_tip.setText(getResources().getString(R.string.cleaning_cache));
		
		
		title_bar_left_ib.setVisibility(View.INVISIBLE);
		
		cacheThread.start();
		
		
	}
	
	private void clearCache(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
			}
		}).start();
	}
	
	private void calSize(){
		 getExternalCacheDir();
	}
	
	private Thread cacheThread = new  Thread(){

		public void run() {
			
			//计算size
			for(String path:pathList){
				
				FileUtils.getFileSize2(path, mCalFileCallback);
			}
			
			try {
				Thread.sleep(PERIOD);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
					mProgressView.setCenterTextVisible(false);
					
					mProgressView.setScoreVisible(true);
					
					mProgressView.setCurrentCount(0.0f);
					
				}
			});

			
			//删除文件
			for(String path:pathList){
				
				FileUtils.deleteFile2(path, mDelFileCallback);
			}
			
			//清楚图片的内存缓存
			ImageCache bitmapCache = ImageCache.getInstants();
			bitmapCache.removeCache();
			
			if(delTotalSize==0){
				
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
					
						mProgressView.setCurrentCount(maxCount);
						
						mProgressView.setScore(maxCount);
						
					}
				});
			}
			
			try {
				Thread.sleep(PERIOD);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
					mProgressView.setScoreVisible(false);
					mProgressView.setCenterTextVisible(true);
					
					float  size   =  (float)(Math.round(totoalSize*1.0f/bytesPerMB*100))/100;
					
					mProgressView.setCenterText(getResources().getString(R.string.cleaned_cache), size+"M");
					
					clear_cache_tip.setText(getResources().getString(R.string.cleaned_cache));
					
					title_bar_left_ib.setVisibility(View.VISIBLE);
					
				}
			});
			
			canBack = true;
			
		};
	};
	
	
	private FileCallback mCalFileCallback = new FileCallback() {
		
		@Override
		public void onTravelfile(File file) {
			
			totoalSize += file.length();
			
			MyLog.d(TAG, "mCalFileCallback size:"+totoalSize);
			
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
						
					float  size   =  (float)(Math.round(totoalSize*1.0f/bytesPerMB*100))/100;
						
					mProgressView.setCenterText(getResources().getString(R.string.clean_cache_calculate), size+"M");
					
				}
			});
			
			
		}
	};
	
	private FileCallback mDelFileCallback = new FileCallback() {
		
		@Override
		public void onTravelfile(File file) {
			
			delTotalSize += file.length();
			
			MyLog.d(TAG, "mDelFileCallback size:"+delTotalSize);
			
			
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
					
					int score = maxCount;
					
					if(totoalSize!=0){
						
						score = (int) (delTotalSize*100/totoalSize);
						
					}
					
					score = score>maxCount?maxCount:score;
									
					mProgressView.setCurrentCount(score);
					
					mProgressView.setScore(score);
					
				}
			});
		}
	};


	@Event(value = { R.id.title_bar_left_ib })
	private void setViewOnClick(View view) {
		switch (view.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		if(canBack){
			AppManager.getInstance().finishActivity(this);
		}
	}
}
